<?php
namespace backend\modules\ding\controllers;

use backend\controllers\BaseController;
use common\models\base\SearchModel;
use common\models\ding\Check;
use common\models\ding\Name;
use common\traits\Curd;
use Yii;

class ChecksController extends BaseController
{
    use Curd;
    /**
     * @var \yii\db\ActiveRecord
     */
    public $modelClass = Name::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $startTime = strtotime(date('Y-m-01', time()));//本月第一天
        $endTime = strtotime(date('Y-m-d H:i:s', time()));//本月第一天
        error_reporting(0);
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['teachers.name'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_ASC,//排序
            ],
            'pageSize' => $this->pageSize,
        ]);
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query;
        $data = Check::find()->andWhere(['>=','userCheckTime',$startTime])->andFilterWhere(['<=','userCheckTime',$endTime])->asArray()->all();
        $arr = [];
        foreach ($data as $v){
            if($v['timeresult']=='Late'||$v['timeresult']=='Early'||$v['timeresult']=='SeriousLate'||$v['timeresult']=='Absenteeism'
                ||$v['timeresult']=='NotSigned'){
                $arr[$v['userId']]+=1;
            }else{
                $arr[$v['userId']]+=0;
            }
        }
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'arr'=>$arr
        ]);
    }
    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        error_reporting(0);
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        //$model->sender_id = Yii::$app->user->id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' => $model,
        ]);
    }
}